/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pig.pigpen.view;

import java.util.Iterator;

import org.apache.pig.backend.executionengine.ExecException;
import org.apache.pig.data.DataBag;
import org.apache.pig.data.DataType;
import org.apache.pig.data.Tuple;
import org.apache.pig.impl.logicalLayer.FrontendException;
import org.apache.pig.impl.logicalLayer.parser.ParseException;
import org.apache.pig.impl.logicalLayer.schema.Schema;

//data container to keep a track of child parent relationship in Tuple and DataBags
public class DataContainer implements IDataContainer {

    private Object data;
    private Object parent;
    private Schema schema;

    public DataContainer(Object data, Object parent, Schema schema) {
        this.data = data;
        this.parent = parent;
        this.schema = schema;
    }

    // public DataContainer(Object data, Object parent) {
    // this.data = data;
    // this.parent = parent;
    // }

    public Object getParent() {
        return parent;
    }

    public Object getData() {
        return data;
    }

    public String getType() {
        return DataType.findTypeName(data);

    }

    public String getName() {
        return schema + data.toString();
    }

    public String getShortName() {
        return data.toString();
    }

    public Object[] getChildren() {
        Object[] result = null;
        if (data instanceof Tuple) {
            Tuple t = (Tuple) data;
            result = new Object[t.size()];
            for (int i = 0; i < t.size(); i++) {
                try {
                    result[i] = new DataContainer(t.get(i), t, new Schema(
                            schema.getField(i)));
                } catch (ExecException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FrontendException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        } else if (data instanceof DataBag) {
            DataBag bag = (DataBag) data;
            result = new Object[(int) bag.size()];
            Iterator<Tuple> it = bag.iterator();
            for (int i = 0; i < bag.size(); i++) {
                try {
                    result[i] = new DataContainer(it.next(), bag, new Schema(
                            schema.getField(0).schema));
                } catch (FrontendException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        }
        return result;
    }
}
